{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import struct\n",
    "import os\n",
    "import shutil\n",
    "import glob\n",
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3135\n"
     ]
    }
   ],
   "source": [
    "labels = os.listdir('/cv/yc/DSGN2/data/ww/allsample/label_2')\n",
    "print(len(labels))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "labels = os.listdir('/cv/yc/DSGN2/data/ww/allsample/label_2')\n",
    "labels = [x.split('.')[0]+'\\n' for x in labels]\n",
    "random.shuffle(labels)\n",
    "\n",
    "# 计算分割点的索引\n",
    "total_length = len(labels)\n",
    "split1_end = int(0.85 * total_length)\n",
    "split2_end = int(0.95 * total_length)\n",
    "\n",
    "# 根据分割点切割列表\n",
    "training = labels[:split1_end]\n",
    "val = labels[split1_end:split2_end]\n",
    "testing = labels[split2_end:]\n",
    "\n",
    "trainval = training+val\n",
    "\n",
    "tf = open('/cv/yc/DSGN2/data/ww/ImageSets/train.txt','a+')\n",
    "for prefix in training:\n",
    "    tf.write(prefix)\n",
    "tf.close()\n",
    "with open('/cv/yc/DSGN2/data/ww/ImageSets/val.txt','a+') as vf:\n",
    "    for prefix in val:\n",
    "        vf.write(prefix)\n",
    "with open('/cv/yc/DSGN2/data/ww/ImageSets/test.txt','a+') as tf:\n",
    "    for prefix in testing:\n",
    "        tf.write(prefix)\n",
    "with open('/cv/yc/DSGN2/data/ww/ImageSets/trainval.txt','a+') as tf:\n",
    "    for prefix in trainval:\n",
    "        tf.write(prefix)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "source_dir = '/cv/yc/DSGN2/data/ww/allsample'\n",
    "destination_dir = '/cv/yc/DSGN2/data/ww/testing'\n",
    "train_txt = '/cv/yc/DSGN2/data/ww/ImageSets/trainval.txt'\n",
    "test_txt = '/cv/yc/DSGN2/data/ww/ImageSets/test.txt'\n",
    "\n",
    "with open(test_txt, \"r\") as test_file:\n",
    "    test_prefixes = test_file.read().splitlines()\n",
    "with open(train_txt, \"r\") as train_file:\n",
    "    train_prefixes = train_file.read().splitlines()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Copy '1692104174750000' \n",
      "Copy '1692268854750000' \n",
      "Copy '1692290175750000' \n",
      "Copy '1692104287750000' \n",
      "Copy '1692272610250000' \n",
      "Copy '1692275493250000' \n",
      "Copy '1692294797250000' \n",
      "Copy '1692272838750000' \n",
      "Copy '1692278313750000' \n",
      "Copy '1692287389750000' \n",
      "Copy '1692104299750000' \n",
      "Copy '1692294811250000' \n",
      "Copy '1692074485250000' \n",
      "Copy '1692285715250000' \n",
      "Copy '1692104293750000' \n",
      "Copy '1692269502750000' \n",
      "Copy '1692074826250000' \n",
      "Copy '1692281878250000' \n",
      "Copy '1692287087750000' \n",
      "Copy '1692290541250000' \n",
      "Copy '1692074883750000' \n",
      "Copy '1692294708250000' \n",
      "Copy '1692294817250000' \n",
      "Copy '1692104363750000' \n",
      "Copy '1692286399750000' \n",
      "Copy '1692287021250000' \n",
      "Copy '1692271290250000' \n",
      "Copy '1692291019750000' \n",
      "Copy '1692271969750000' \n",
      "Copy '1692290902250000' \n",
      "Copy '1692286111750000' \n",
      "Copy '1692074493250000' \n",
      "Copy '1692104502750000' \n",
      "Copy '1692290305250000' \n",
      "Copy '1692278089750000' \n",
      "Copy '1692294066750000' \n",
      "Copy '1692291432750000' \n",
      "Copy '1692281837750000' \n",
      "Copy '1692269460250000' \n",
      "Copy '1692278258750000' \n",
      "Copy '1692074282750000' \n",
      "Copy '1692290256750000' \n",
      "Copy '1692272824750000' \n",
      "Copy '1692288306250000' \n",
      "Copy '1692288960250000' \n",
      "Copy '1692286845250000' \n",
      "Copy '1692295618250000' \n",
      "Copy '1692272914250000' \n",
      "Copy '1692277061250000' \n",
      "Copy '1692271925250000' \n",
      "Copy '1692275400250000' \n",
      "Copy '1692294673750000' \n",
      "Copy '1692286980750000' \n",
      "Copy '1692279881750000' \n",
      "Copy '1692290555250000' \n",
      "Copy '1692270908250000' \n",
      "Copy '1692288432750000' \n",
      "Copy '1692275533750000' \n",
      "Copy '1692294060750000' \n",
      "Copy '1692142015250000' \n",
      "Copy '1692104618750000' \n",
      "Copy '1692295466750000' \n",
      "Copy '1692276964250000' \n",
      "Copy '1692278254750000' \n",
      "Copy '1692278366250000' \n",
      "Copy '1692287073750000' \n",
      "Copy '1692272598250000' \n",
      "Copy '1692291104250000' \n",
      "Copy '1692292075750000' \n",
      "Copy '1692274162250000' \n",
      "Copy '1692294583750000' \n",
      "Copy '1692275197750000' \n",
      "Copy '1692289489250000' \n",
      "Copy '1692290278750000' \n",
      "Copy '1692279716250000' \n",
      "Copy '1692278075750000' \n",
      "Copy '1692286216250000' \n",
      "Copy '1692293965250000' \n",
      "Copy '1692274108750000' \n",
      "Copy '1692279811250000' \n",
      "Copy '1692281807750000' \n",
      "Copy '1692285759750000' \n",
      "Copy '1692287549250000' \n",
      "Copy '1692281882250000' \n",
      "Copy '1692285830750000' \n",
      "Copy '1692285810250000' \n",
      "Copy '1692279867750000' \n",
      "Copy '1692285889250000' \n",
      "Copy '1692277039250000' \n",
      "Copy '1692292615750000' \n",
      "Copy '1692281486250000' \n",
      "Copy '1692141386250000' \n",
      "Copy '1692268964750000' \n",
      "Copy '1692286099750000' \n",
      "Copy '1692287401750000' \n",
      "Copy '1692295577750000' \n",
      "Copy '1692272806250000' \n",
      "Copy '1692274406750000' \n",
      "Copy '1692104236250000' \n",
      "Copy '1692286095750000' \n",
      "Copy '1692279809250000' \n",
      "Copy '1692287033250000' \n",
      "Copy '1692295484750000' \n",
      "Copy '1692292136250000' \n",
      "Copy '1692275080750000' \n",
      "Copy '1692141307250000' \n",
      "Copy '1692104226250000' \n",
      "Copy '1692285834750000' \n",
      "Copy '1692268868750000' \n",
      "Copy '1692279595250000' \n",
      "Copy '1692290262750000' \n",
      "Copy '1692276381250000' \n",
      "Copy '1692278144250000' \n",
      "Copy '1692275349750000' \n",
      "Copy '1692289321750000' \n",
      "Copy '1692291313250000' \n",
      "Copy '1692287177250000' \n",
      "Copy '1692278242750000' \n",
      "Copy '1692288292250000' \n",
      "Copy '1692279790750000' \n",
      "Copy '1692294829250000' \n",
      "Copy '1692295266750000' \n",
      "Copy '1692289824750000' \n",
      "Copy '1692290226250000' \n",
      "Copy '1692288463250000' \n",
      "Copy '1692281781250000' \n",
      "Copy '1692271838250000' \n",
      "Copy '1692279819250000' \n",
      "Copy '1692281720750000' \n",
      "Copy '1692293251750000' \n",
      "Copy '1692294813250000' \n",
      "Copy '1692274293750000' \n",
      "Copy '1692286877750000' \n",
      "Copy '1692289613750000' \n",
      "Copy '1692271830250000' \n",
      "Copy '1692289002750000' \n",
      "Copy '1692272679250000' \n",
      "Copy '1692292126250000' \n",
      "Copy '1692104369750000' \n",
      "Copy '1692281738750000' \n",
      "Copy '1692291384250000' \n",
      "Copy '1692277140250000' \n",
      "Copy '1692104244250000' \n",
      "Copy '1692278120250000' \n",
      "Copy '1692295601750000' \n",
      "Copy '1692291347250000' \n",
      "Copy '1692142058750000' \n",
      "Copy '1692291035750000' \n",
      "Copy '1692271173750000' \n",
      "Copy '1692074560250000' \n",
      "Copy '1692271177750000' \n",
      "Copy '1692141465250000' \n",
      "Copy '1692288509750000' \n",
      "Copy '1692288572250000' \n",
      "Copy '1692288850250000' \n",
      "Copy '1692294669750000' \n",
      "Copy '1692277002750000' \n"
     ]
    }
   ],
   "source": [
    "for prefix in test_prefixes:\n",
    "    source_files = glob.glob(f'{source_dir}/**/{prefix}*')\n",
    "    \n",
    "    for source_path in source_files:\n",
    "        relative_path = os.path.relpath(source_path, source_dir)\n",
    "        dest_path = os.path.join(destination_dir, relative_path.replace(\"allsample\", \"testing\"))\n",
    "        \n",
    "        dest_dir_path = os.path.dirname(dest_path)\n",
    "        os.makedirs(dest_dir_path, exist_ok=True)\n",
    "        \n",
    "        shutil.copy2(source_path, dest_path)\n",
    "    print(f\"Copy '{prefix}' \")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2978"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(train_prefixes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "destination_dir = '/cv/yc/DSGN2/data/ww/training'\n",
    "\n",
    "for prefix in train_prefixes:\n",
    "    source_files = glob.glob(f'{source_dir}/**/{prefix}*')\n",
    "    # print(source_files)\n",
    "    \n",
    "    for source_path in source_files:\n",
    "        relative_path = os.path.relpath(source_path, source_dir)\n",
    "        # print(destination_dir)\n",
    "        dest_path = os.path.join(destination_dir, relative_path.replace(\"allsample\", \"training\"))\n",
    "        # print(dest_path)\n",
    "        \n",
    "        dest_dir_path = os.path.dirname(dest_path)\n",
    "        os.makedirs(dest_dir_path, exist_ok=True)\n",
    "        \n",
    "        shutil.copy2(source_path, dest_path)\n",
    "    #     print(f\"Copy '{source_path}' to {dest_path} \")\n",
    "    #     break\n",
    "    # break"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "bev",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.17"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
